﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Forms;
using Bookvar.ObjectModel;
using Bookvar.UI.ViewModel;
using Binding=System.Windows.Data.Binding;
using Button=System.Windows.Controls.Button;

namespace BookvarWpf.Windows
{
    

    /// <summary>
    /// Interaction logic for SampleWindow.xaml
    /// </summary>
    public partial class AddAttachmentsWindow
    {
        private readonly AttachmentsWindowViewModel viewModel;

        public AddAttachmentsWindow( Topic topic, MindMap mindmap )
        {
            InitializeComponent();

            viewModel = new AttachmentsWindowViewModel( mindmap, topic );
            ViewModelBase.AddCommandAndInputBindings( this, viewModel );

            fileList.SetBinding( ItemsControl.ItemsSourceProperty, viewModel.CreateTopicAttachmentsBinding() );
            fileList.SetBinding( Selector.SelectedItemProperty,
                                 new Binding("SelectedAttachment") { Source = viewModel, Mode = BindingMode.TwoWay });
        }

        private void OnAddClicked(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog { Filter = "Any file type (*.*)|*.*" };
            fileDialog.InitialDirectory = Environment.GetFolderPath( Environment.SpecialFolder.Personal );

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                ((Button)sender).CommandParameter = fileDialog.FileName;
            }
        }

        private void OnSaveAsClicked(object sender, RoutedEventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = "|" + Path.GetExtension( viewModel.SelectedAttachment.FileName );

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                ( (Button) sender ).CommandParameter = fileDialog.FileName;
            }
        }
    }
}